#include "asm.h"
#include "memlayout.h"
#include "mmu.h"

# Start the first CPU: switch to 32-bit protected mode, jump into C.
# The BIOS loads this code from the first sector of the hard disk into
# memory at physical address 0x7c00 and starts executing in real mode
# with %cs=0 %ip=7c00.   At&t的汇编格式

.code16                       # Assemble for 16-bit mode
.globl start
start:
  cli                         # BIOS enabled interrupts; disable  关闭中断

  # Zero data segment registers DS, ES, and SS.
  xorw    %ax,%ax             # Set %ax to zero 
  movw    %ax,%ds             # -> Data Segment
  movw    %ax,%es             # -> Extra Segment
  movw    %ax,%ss             # -> Stack Segment     将所有的寄存器清零

  # Physical address line A20 is tied to zero so that the first PCs 
  # with 2 MB would run software that assumed 1 MB.  Undo that.
seta20.1:
  inb     $0x64,%al               # Wait for not busy  inb指令从io端口读取一个字节  表示从0x64端口读取一个到al
  testb   $0x2,%al                # 将两个操作数进行逻辑与运算，并根据结果设置标志位ZF位，如果结果为0设置ZF为 1
  jnz     seta20.1                # 结果不为零发生跳转

  #上面的解释：如果al是高位testb指令中ZF为0，不为零不发生跳转，则A20开启。

  movb    $0xd1,%al               # 0xd1 -> port 0x64
  outb    %al,$0x64               # outb指令，向io端口发送一个字节 

seta20.2:
  inb     $0x64,%al               # Wait for not busy  
  testb   $0x2,%al                # 
  jnz     seta20.2

  movb    $0xdf,%al               # 0xdf -> port 0x60
  outb    %al,$0x60               

  #outb指令，第一个outb指令向端口0x64发送0xd1，第二个指令向0x60端口发送0xdf
  #这两个发送的字符在keyboard中解释？？？

  # Switch from real to protected mode.  Use a bootstrap GDT that makes
  # virtual addresses map directly to physical addresses so that the
  # effective memory map doesn't change during the transition.
  # 使用引导GDT使虚拟地址直接映射到物理地址，这样有效的内存映射在转换期间不会改变
  lgdt    gdtdesc             # lgdt加载全局描述符表
  movl    %cr0, %eax          # cr0包含保护模式，分页模式的开启
  orl     $CR0_PE, %eax       # PE是保护模式
  movl    %eax, %cr0

  # Complete the transition to 32-bit protected mode by using a long jmp
  # to reload %cs and %eip.  The segment descriptors are set up with no
  # translation, so that the mapping is still the identity mapping.
  # 使用长jmp重新加载%cs和%eip，完成到32位保护模式的转换。段描述符的设置没有转换，因此映射仍然是标识映射
  ljmp    $(SEG_KCODE<<3), $start32    # ljmp长转移指令，转移到下面32位代码

  #ljmp 段选择子，段内偏移。 段选择子为8对应gdt中的代码段，start32对应段内偏移。刚好指到下面32位代码
  # ljmp  $8, $start32

.code32  # Tell assembler to generate 32-bit code now.
start32:
  # Set up the protected-mode data segment registers
  movw    $(SEG_KDATA<<3), %ax    # Our data segment selector movw   $16, %ax
  movw    %ax, %ds                # -> DS: Data Segment
  movw    %ax, %es                # -> ES: Extra Segment
  movw    %ax, %ss                # -> SS: Stack Segment
  movw    $0, %ax                 # Zero segments not ready for use
  movw    %ax, %fs                # -> FS
  movw    %ax, %gs                # -> GS  清零

  # Set up the stack pointer and call into C.
  movl    $start, %esp
  call    bootmain       

  # If bootmain returns (it shouldn't), trigger a Bochs
  # breakpoint if running under Bochs, then loop.
  movw    $0x8a00, %ax            # 0x8a00 -> port 0x8a00
  movw    %ax, %dx
  outw    %ax, %dx
  movw    $0x8ae0, %ax            # 0x8ae0 -> port 0x8a00
  outw    %ax, %dx                
spin:                             # spin是为了bootmain异常返回时备用计划
  jmp     spin
  

# Bootstrap GDT
.p2align 2                                # force 4 byte alignment
gdt:                        # 低16位是全局描述符表（GDT）的界限值，高32位是GDT的基地址
  SEG_NULLASM                             # null seg 空项
  SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff)   # code seg 代码段   SEG_ASM(0xA, 0x0, 0xffffffff)    
  SEG_ASM(STA_W, 0x0, 0xffffffff)         # data seg 数据段 起始地址都为0x0,段界限都为0xffffffff（也就是4GB）SEG_ASM(0x2, 0x0, 0xffffffff)

// GDTR 寄存器一共 48 位，其中高 32 位用来存储我们的 GDT 在内存中的位置，
// 其余的低 16 位用来存我们的 GDT 有多少个段描述符。
gdtdesc:
  .word   (gdtdesc - gdt - 1)             # gdtdesc的地址减去gdt的地址再减去一，sizeof(gdt) - 1
  .long   gdt                             # address gdt
